package com.example.youyouhaodian.controller;

import com.example.youyouhaodian.pojo.User;
import com.example.youyouhaodian.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
@Api(tags = "用户")
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @ApiOperation(value = "实现登录", notes = "1.判断用户名是否存在 2.判断用户名和密码是否匹配")
    @RequestMapping(value = "/findUserName",method = RequestMethod.POST)
    public String findUserName(@RequestParam("userName")String userName, String userPwd, HttpSession session) {
        User user1=userService.findUserName(userName);
        if(user1!=null){
            if(user1.getUser_Pwd().equals(userPwd)){
                if(session.getAttribute("userId")!=null){
                    session.removeAttribute("userId");
                }
                session.setAttribute("userId",user1.getUser_Id());
                return "登录成功";

            }else {
                return "用户名和密码不正确";
            }

        }
        return "用户名不存在";
    }

    @ApiOperation(value = "实现注册", notes = "增加用户")
    @RequestMapping(value = "/insertUser",method = RequestMethod.POST)
    public String insertUser(@RequestParam("userName")String userName,String userPwd){
        if(userService.findUserName(userName)!=null) {
            return "该用户名已存在";
        }
        int i=userService.insertUser(userName,userPwd);
        if(i>0){
            return "注册成功";
        }
        return "注册失败";
    }

}

